package com.eff.android.lib.asynctask;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.database.sqlite.SQLiteException;
import android.os.AsyncTask;
import android.util.Log;

public class AsyncTaskEF extends AsyncTask<String, String, String> {

	final String TAG = "AsyncTaskEF.java";
	
	// set your json string url here
    String yourJsonStringUrl = "http://pronounce.cf/api/load/100";
    
    // contacts JSONArray
    JSONArray dataJsonArr = null;
    
    // Database Helper
 	DatabaseHelper db;
 		
 	private Context mContext;
    
    public AsyncTaskEF(Context context) {
    	super();
    	mContext = context;
    }
    
    @Override
    protected void onPreExecute() {
    	super.onPreExecute();
    }
    
    @Override
    protected String doInBackground(String... arg0) {

    	// Create List Model
    	List<Model> listModels = new ArrayList<Model>();
    	
        try {
            
            // instantiate our json parser
            JsonParser jParser = new JsonParser();                              
            
            // get json string from url
            //JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);
            dataJsonArr = jParser.getJSONFromUrl(yourJsonStringUrl);
            
            // get the array of users
            //dataJsonArr = json.getJSONArray(null);
           
            // loop through all users
            for (int i = 0; i < dataJsonArr.length(); i++) {

                JSONObject c = dataJsonArr.getJSONObject(i);

                // Storing each json item in variable
                String id_hinh = c.getString("id_hinh");
                String van_ban = c.getString("van_ban");
                String y_nghia = c.getString("y_nghia");
                String uri_hinh = c.getString("uri_hinh");
                String phien_am_quoc_te = c.getString("phien_am_quoc_te");
                String loai_hinh = c.getString("loai_hinh");
                String ma_chu_de = c.getString("ma_chu_de");
                String date_create = c.getString("date_create");
                
                // Create Model
                Model tempModel = new Model(id_hinh, van_ban, y_nghia, uri_hinh, phien_am_quoc_te,
                		loai_hinh, ma_chu_de, date_create);
                
                //Add Model to List
                listModels.add(tempModel);
                 
                /*
                // show the values in our logcat
                Log.e(TAG, "id_hinh: " + tempModel.getIdHinh() 
                        + ", van_ban: " + tempModel.getVanBan()
                        + ", y_nghia: " + tempModel.getYNghia()
                        + ", uri_hinh: " + tempModel.getUriHinh()
                        + ", phien_am_quoc_te: " + tempModel.getPhienAmQuocTe()
                        + ", loai_hinh: " + tempModel.getLoaiHinh()
                        + ", ma_chu_de: " + tempModel.getMaChuDe()
                        + ", date_create: " + tempModel.getDateCreate());*/
            }
            	
            //Log.d("Pass JSON : ", String.valueOf(listModes.size()));
        } catch (JSONException e) {
            e.printStackTrace();
        }               
    
        try {
        
	        db = new DatabaseHelper(mContext);
	    	
	        // Inserting models in database
	        	
			for (int i = 0; i < listModels.size(); i++)
			{
				/*
				Log.e(TAG, "id_hinh: " + listModels.get(i).getIdHinh() 
	                    + ", van_ban: " + listModels.get(i).getVanBan()
	                    + ", y_nghia: " + listModels.get(i).getYNghia()
	                    + ", uri_hinh: " + listModels.get(i).getUriHinh()
	                    + ", phien_am_quoc_te: " + listModels.get(i).getPhienAmQuocTe()
	                    + ", loai_hinh: " + listModels.get(i).getLoaiHinh()
	                    + ", ma_chu_de: " + listModels.get(i).getMaChuDe()
	                    + ", date_create: " + listModels.get(i).getDateCreate());*/
				db.createModel(listModels.get(i));
			}
			
	        Log.e("Model Count", "Model count: " + db.getModelCount());
	 	        
	        /* // Test Getting all Models
	        Log.d("Get Models", "Getting All Models");
	        
	        List<Model> allModels = db.getAllModels();
	        for (Model model : allModels) {
	            Log.d("Model", model.getIdHinh());
	        }*/        
	        
	        /* // Test Getting Single Models
	        Model model = new Model();
	        model = db.getModel("147200");
	        Log.d("Single Model : ", model.getIdHinh());
	        */
	        
	        /*
	        // Test Deleting a Model
	        Log.d("Delete Model", "Deleting a Model");
	        Log.d("Tag Count", "Tag Count Before Deleting: " + db.getModelCount());
	 
	        List<Model> allModels = db.getAllModels();
	        for (int i = 0; i < db.getModelCount(); i++)
	        {
	        	db.deleteModel(allModels.get(i).getIdHinh());
	        }
	 
	        Log.d("Tag Count", "Tag Count After Deleting: " + db.getModelCount());
	        */
	        
	        // Don't forget to close database connection
	        db.closeDB();
	        
		} catch (SQLiteException e) {
	        e.printStackTrace();
	    }        
        
        return null;
    }
    
    @Override
    protected void onPostExecute(String strFromDoInBg) {
    	super.onPostExecute(strFromDoInBg);
    }
}
